<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of AuthenticationUtils
 *
 * @author ebsi
 */
class AuthenticationUtils {

    //get customerId
    static function getCurrentCustomer() {
        if (!isset($_SESSION)) {
            session_start();
        }
        return $_SESSION['customer']->getID();
    }

    // store front login 
    static function checkSFLogin() {
        if (!isset($_SESSION)) {
            session_start();
        }
        if (isset($_SESSION['customer'])) {
            return true;
        }
        return false;
    }

    static function logout() {
        if (!isset($_SESSION)) {
            session_start();
        }
        if (isset($_SESSION['customer'])) {
            $_SESSION['customer'] = null;
        }
    }

    // authenticate SF login
    static function authenticateSFLogin() {
        if ($_SERVER['REQUEST_METHOD'] == 'POST') {
            if (!isset($_SESSION)) {
                session_start();
            }
            $username = $_POST['username'];
            $password = $_POST['password'];
            $user = R::findOne('customercredential', ' username = :username  and password = :password', array(':username' => $username, ':password' => md5($password)));
            if (!empty($user)) {
                $repository = BO::getCustomerBORepository();
                $customerBO = $repository->getCustomerBO($user->customerid);
                $_SESSION['customer'] = $customerBO;
            } else {
                header("location:login.php?loginError=true");
            }
        }
    }

}
